#-*-coding=utf-8-*-
from django.shortcuts import render_to_response
from django.http import *
from django.template import *
from auth.decorators import *
from models import *
from forms import *
from google.appengine.ext import db

def index(request, template):
    news = News.all().order("-added")
    return render_to_response(template, {"objs":news}, context_instance=RequestContext(request))


def page(request, template, key = None):
    news = News.get(key)
    return render_to_response(template, {"obj":news}, context_instance=RequestContext(request))


@admin_required
def admin_list(request, template):
    news = News.all().order("-added")
    return render_to_response(template, {"objs":news}, context_instance=RequestContext(request))


@admin_required
def admin_save(request, template, key = None):
    if request.method == "GET":
        if key == None or key == "":
            form = NewsForm()
        else:
            news = News.get(key)
            form = NewsForm(instance = news)
        return render_to_response(template, {"form":form, "next":request.GET.get("next", ".")}, context_instance=RequestContext(request))
    else:
        if key == None or key == "":
            form = NewsForm(request.POST)
        else:
            form = NewsForm(request.POST, instance = News.get(key))
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(request.POST.get("next", "."))
        else:
            return render_to_response(template, {"form":form, "next":request.POST.get("next", ".")}, context_instance=RequestContext(request))


@admin_required
def admin_delete(request, key):
    News.get(key).delete()
    return HttpResponseRedirect(request.REQUEST.get("next", "."))


@admin_required
def admin_comment_list(request, template, key):
    news = News.get(key)
    comments = news.newspaper_comment_news
    return render_to_response(template, {"objs":comments, "parent":news}, context_instance=RequestContext(request))


@admin_required
def admin_comment_save(request, template, key = None):
    if request.method == "GET":
        parent = request.GET.get("parent", None)
        if key == None or key == "":
            form = CommentForm(initial = {"news":parent})
        else:
            comment = Comment.get(key)
            form = CommentForm(instance = comment)
        return render_to_response(template, {"form":form, "next":request.GET.get("next", ".")}, context_instance=RequestContext(request))
    else:
        if key == None or key == "":
            form = CommentForm(request.POST)
        else:
            form = CommentForm(request.POST, instance = Comment.get(key))
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(request.POST.get("next", "."))
        else:
            return render_to_response(template, {"form":form, "next":request.POST.get("next", ".")}, context_instance=RequestContext(request))


@admin_required
def admin_comment_delete(request, key):
    Comment.get(key).delete()
    return HttpResponseRedirect(request.REQUEST.get("next", "."))


def add(request, template, parent):
    if request.method == "GET":
        return render_to_response(template, {"news_key":parent, "next":request.GET.get("next", ".")}, context_instance=RequestContext(request))
    else:
        form = CommentForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(request.POST.get("next", "."))
        else:
            return render_to_response(template, {"news_key":request.POST["news"],
                                                 "comment":request.POST["comment"],
                                                 "next":request.POST.get("next", ".")}, context_instance=RequestContext(request))

